/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package polybook.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.table.AbstractTableModel;
import polybook.model.Person;
import polybook.utils.SortUtils;

/**
 *
 * @author kazanova
 */
public class PersonJTableModel extends AbstractTableModel {

    protected ArrayList<Person> modelDatas;
    private final static int COLUMN_PSEUDO = 0;
    private final static int COLUMN_PRENOM = 1;
    private final static int COLUMN_NOM = 2;
    private final static int COLUMN_DATE = 3;
    private final static String[] columnNames = {"Pseudo", "Prenom", "Nom", "Date de naissance"};
    private final static SimpleDateFormat dateNaissFormater = new SimpleDateFormat("dd/MM/yyyy");

    public PersonJTableModel() {
        this.modelDatas = new ArrayList<Person>();
    }

    public PersonJTableModel(ArrayList<Person> datas) {
        this.modelDatas = datas;
    }

    public void addElement(Person newPerson) {
        // Adds the element in the last position in the list
        modelDatas.add(newPerson);
        fireTableRowsInserted(modelDatas.size() - 1, modelDatas.size() - 1);
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        return modelDatas.size();
    }

    @Override
    public int getColumnCount() {
        // 4 Colonnes : Pseudo, prenom, nom, date de naissance
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object value;
        switch (columnIndex) {
            case COLUMN_PRENOM:
                value = modelDatas.get(rowIndex).getPrenom();
                break;
            case COLUMN_NOM:
                value = modelDatas.get(rowIndex).getNom();
                break;
            case COLUMN_DATE:
                value = dateNaissFormater.format(modelDatas.get(rowIndex).getDateNaiss());
                break;
            default:
                // Par defaut on retourne le pseudo
                value = modelDatas.get(rowIndex).getPseudo();
                break;
        }

        return value;
    }
    
    public ArrayList<Person> getData() {
        return modelDatas;
    }
    
    public void quickNameSort(){
         Collections.sort(this.modelDatas, SortUtils.compParNomPrenom);
         this.fireTableDataChanged();
    }
    
    public void quickFriendSort(){
        Collections.sort(this.modelDatas, SortUtils.compParDegre);
        this.fireTableDataChanged();
    }
    
    public void slowNameSort(){
        SortUtils.BubbleSortByName(this.modelDatas);
        this.fireTableDataChanged();
    }
    
    public void slowFriendSort(){
        SortUtils.BubbleSortByFriends(this.modelDatas); 
        this.fireTableDataChanged();
    }
    
    public void shuffleDatas() {
        Collections.shuffle(this.modelDatas);
        this.fireTableDataChanged();
    }
}
